package com.gsbelarus.gedemin.income.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

import com.gsbelarus.gedemin.income.table.ClientsTable;
import com.gsbelarus.gedemin.income.R;

import java.util.ArrayList;
import java.util.List;

public class ClientsListAdapter extends ArrayAdapter<ClientsTable> {

    private List<ClientsTable> contacts;
    private ClientsListSimpleAdapter adapter;

    public static final int TOTAL_SUM = -1;

    public static final int ONLY_PAID = 0;
    public static final int ONLY_PLAN = 1;
    public static final int PAID_WITH_NULL_PLAN = 2;
    public static final int PLAN_WITH_NULL_PAID = 3;
    public static final int ALL_VISIBLE = 4;
    public static final int ALL_INVISIBLE = 5;
    public static final int ALL_GONE = 6;

    private int[] arrayColor = new int[]{R.color.list_item_one, R.color.list_item_two};

    public ClientsListAdapter(Context context, List<ClientsTable> contacts) {
        super(context, R.layout.clients_list_item, new ArrayList<ClientsTable>());
        this.contacts = contacts;
        addAll(contacts);

        adapter = new ClientsListSimpleAdapter(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ClientsTable item = getItem(position);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.clients_list_item, null);

            switch (getItemViewType(position)) {
                case ONLY_PAID:
                    adapter.changeVisibilityPaidText(convertView, View.VISIBLE);
                    break;
                case ONLY_PLAN:
                    adapter.changeVisibilityPlanText(convertView, View.VISIBLE);
                    adapter.changeVisibilityPlanImage(convertView, View.INVISIBLE);
                    break;
                case PAID_WITH_NULL_PLAN:
                    adapter.changeVisibilityPaidText(convertView, View.VISIBLE);
                    adapter.changeVisibilityPlanText(convertView, View.INVISIBLE);
                    adapter.changeVisibilityPlanImage(convertView, View.INVISIBLE);
                    break;
                case PLAN_WITH_NULL_PAID:
                    adapter.changeVisibilityPaidText(convertView, View.VISIBLE);
                    adapter.changeVisibilityPlanText(convertView, View.VISIBLE);
                    adapter.changeVisibilityPercent(convertView, View.INVISIBLE);
                    adapter.changeVisibilityPlanImage(convertView, View.INVISIBLE);
                    break;
                case ALL_VISIBLE:
                    adapter.changeVisibilityPaidText(convertView, View.VISIBLE);
                    adapter.changeVisibilityPlanText(convertView, View.VISIBLE);
                    adapter.changeVisibilityPercent(convertView, View.VISIBLE);
                    adapter.changeVisibilityPlanImage(convertView, View.VISIBLE);
                    break;
                case ALL_INVISIBLE:
                    adapter.changeVisibilityPaidText(convertView, View.INVISIBLE);
                    adapter.changeVisibilityPlanText(convertView, View.INVISIBLE);
                    adapter.changeVisibilityPercent(convertView, View.INVISIBLE);
                    adapter.changeVisibilityPlanImage(convertView, View.INVISIBLE);
                    break;
                case ALL_GONE:
                    break;
            }
        }
        if (getItem(position).getId() == TOTAL_SUM)
            convertView.setBackgroundColor(getContext().getResources().getColor(R.color.graph_plan));
        else {
            int i = position % arrayColor.length;
            convertView.setBackgroundColor(getContext().getResources().getColor(arrayColor[i]));
        }
        adapter.adaptView(convertView, item);

        TextView countText = (TextView) convertView.findViewById(R.id.client_list_item_count);
        if (countText != null)
            countText.setText(String.valueOf(position + 1));
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        ClientsTable item = getItem(position);

        int mod = item.getRenderingMod();
        /** если флаг - ALL_VISIBLE и плана нет, то отображаем с флагом PAID_WITH_NULL_PLAN */
        if (mod == ALL_VISIBLE && item.getPlanSum() <= 0)
            return PAID_WITH_NULL_PLAN;

        /** для будущих периодов не отображаем иконку*/
        if (mod == ALL_VISIBLE && item.getPlanSum() > 0 && item.getPaidSum() <= 0 && adapter.isFuturePeriod(item))
            return PLAN_WITH_NULL_PAID;

        return mod;
    }

    @Override
    public int getViewTypeCount() {
        return 7;
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).getId();
    }

    private Filter filter = new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults result = new FilterResults();
            if (constraint != null && constraint.length() > 0) {
                List<ClientsTable> founded = new ArrayList<ClientsTable>();
                for (ClientsTable p : contacts) {
                    if (p.getName().toUpperCase().contains(constraint.toString().toUpperCase()))
                        founded.add(p);
                }
                result.values = founded;
                result.count = founded.size();
            } else {
                result.values = contacts;
                result.count = contacts.size();
            }
            return result;
        }

        @Override
        @SuppressWarnings("unchecked")
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            clear();
            addAll((List<ClientsTable>) results.values);
            notifyDataSetChanged();
        }
    };

    @Override
    public Filter getFilter() {
        return filter;
    }
}